package org.NooLab.structures.randomgraph;

import java.util.ArrayList;






public class ParticledText implements ParticledTextIntf{

	/** list of unique words  */
	ArrayList<String>  words = new ArrayList<String>();
	
	/**  unique words (each one occurs only once), and for each word we have a list of positions !!  */
	WordPositions      collWordPositions = new WordPositions();   
	
	/** each word of the text and its position, such that a concatenation 
	 * would yield the text without delimiters */
	RawEntityPositions wordPositions = new RawEntityPositions();
	
	/** each sentence of the text and its position */
	RichEntityPositions sentencePositions = new RichEntityPositions();
	ArrayList<Integer>  sentence1stPosIndexes = new ArrayList<Integer>();
	
	/** references quotations and citations 
	 *  - quotations : max 5 words && not a sentence
	 *  - citations  : >=6 words or full sentences or containing "[...]"  */
	RichEntityPositions quotcitPositions = new RichEntityPositions();
	
	RawEntityPositions pageLocations = new RawEntityPositions (); // possible with Tika ??
	
	WordPositions      names = new WordPositions(); // only possible for non-German
	
	boolean isCapitalNounLanguage = false;
	
	GuidObject         guids = new GuidObject(); 
	String             filename= "" ;                
	
	String             rawText = ""; // as saved to temp file or DB in zipped form
	
	// ========================================================================
	public ParticledText(){
		
	}
	public void clear() {

		if (words!=null){
			words.clear();
		}
		if (collWordPositions!=null){
			collWordPositions.clear() ;   
		}
		if (wordPositions != null) {
			wordPositions.clear();
		}
		if (sentencePositions != null) {
			sentencePositions.clear();
		}
		if (pageLocations != null) {
			pageLocations.clear();
		}		
		
	}
	// ========================================================================	

	@Override
	public void consolidateRawWordPositions() {
		 
		/*
		getWordPositions();
			// unique words (each one occurs only once), and for each word we have a list of positions !!  
			// WordPositions
		getCollWordPositions();
		getSentencePositions();
		getWords(); // == unique !
		*/
		RawEntityPosition rwp;
		WordPosition      cwp;
		String str ;
		int p, chrpos;
		
		// determine unique words, just as they flow in 
		for (int i=0;i<wordPositions.size();i++){
			
			rwp = wordPositions.getItem(i) ;
			
			if ((rwp==null) || (rwp.getWordStr().length()==0)){
				continue;
				// no collWordPosition will be opened
			}
			
			// it is desync'ed!!! due to removals pos values are 
			// too large, more and more for higher index values
			// we have to resync it here
			rwp.startByEntity = i;
			// for Indo-European languages:
			// changing initial capital to lower case, mainly for the beginning of sentences...
			// needs performing the SentenceChunker in advance... 
			// later (by plugin) except named entities (requires dictionary)
			homogenizeWords(rwp) ;
			
			str = rwp.textualStr ;
			
			if (str.length()==0){
				continue;
			}
			
			p = words.indexOf(str);
			if (p<0){
				words.add(str) ;
				
				cwp = new WordPosition()  ;
				cwp.wordStr = str;
				
				int candPos = rwp.startByEntity;
				
				cwp.positions.add( candPos ) ;

				chrpos = rwp.getStartByEntitySub();
				cwp.charPositions.add(chrpos) ;
				
				collWordPositions.add(cwp) ;
			}else{ 
				chrpos = rwp.getStartByEntitySub();

				cwp = collWordPositions.get(p) ;
				cwp.charPositions.add(chrpos) ;
				
				cwp.positions.add( rwp.startByEntity ) ;
			}
			
		}// i->
		
	}

	private String homogenizeWords(RawEntityPosition rwp) {
		// TODO Auto-generated method stub
		return null;
	}
	public String getRawText() {
		return rawText;
	}
	public RawEntityPositions getWordPositions() {
		return wordPositions;
	}

	public ArrayList<String> getWords() {
		return words;
	}

	public WordPositions getCollWordPositions() {
		if (collWordPositions==null){
			collWordPositions = new WordPositions();
		}
		return collWordPositions;
	}

	public RichEntityPositions getSentencePositions() {
		return sentencePositions;
	}

	public void setRawText(String rawText) {
		this.rawText = rawText;
	}


	public void setWords(ArrayList<String> words) {
		this.words = words;
	}
	public GuidObject getGuids() {
		return guids;
	}


	public void setGuids(GuidObject guids) {
		this.guids = guids;
	}


	public String getFilename() {
		return filename;
	}


	public void setFilename(String filename) {
		this.filename = filename;
	}

	public void setCollWordPositions(WordPositions collWordPositions) {
		this.collWordPositions = collWordPositions;
	}


	public void setWordPositions(RawEntityPositions wordPositions) {
		this.wordPositions = wordPositions;
	}


	public void setSentencePositions(RichEntityPositions sentencePos) {
		sentencePositions = sentencePos;
	}


	public RawEntityPositions getPageLocations() {
		return pageLocations;
	}


	public void setPageLocations(RawEntityPositions pageLocations) {
		this.pageLocations = pageLocations;
	}
	
}
